package org.client.iot.ui.device;

import static org.client.iot.constant.GlobeConstant.PAGE_DEFINE.*;

import android.bluetooth.BluetoothProfile;
import android.widget.RadioGroup;

import androidx.lifecycle.Observer;
import androidx.viewpager2.widget.ViewPager2;

import com.bumptech.glide.Glide;

import org.client.iot.R;
import org.client.iot.ble.BleTemplate;
import org.client.iot.constant.GlobeDataManager;
import org.client.iot.databinding.ActivityDeviceOptionBinding;
import org.client.iot.ui.ble.BindBleActivity;
import org.client.iot.ui.device.adapter.BottomMenuAdapter;
import org.client.iot.ui.project.ProjectManagerActivity;
import org.tzw.template.base.BaseActivity;

public class DeviceOptionActivity extends BaseActivity<ActivityDeviceOptionBinding> implements RadioGroup.OnCheckedChangeListener {
    private BottomMenuAdapter mAdapter;
    private String devName;

    @Override
    protected void initData() {
        mAdapter = new BottomMenuAdapter(getSupportFragmentManager(), getLifecycle());
        devName = getIntent().getStringExtra("devName");
    }

    @Override
    protected void initView() {
        setSupportActionBar(mRoot.toolbarDeviceOption);
        mRoot.textViewTitle.setText(devName == null ? "" : devName);
        mRoot.viewPagerOptionFragment.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);
        mRoot.viewPagerOptionFragment.setAdapter(mAdapter);
        mRoot.viewPagerOptionFragment.setCurrentItem(0);
        mRoot.viewPagerOptionFragment.setOffscreenPageLimit(2);

        mRoot.rbMenuDevDebugger.setChecked(true);
    }


    @Override
    protected void initListener() {
        mRoot.rgBottomMenu.setOnCheckedChangeListener(this);
        mRoot.viewPagerOptionFragment.registerOnPageChangeCallback(onPageChangeCallback);
        mRoot.imgBack.setOnClickListener(v -> finish());
        GlobeDataManager.bluetoothProfileLiveData.observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer status) {
                updateConnectImage(status);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        updateConnectImage(BleTemplate.getInstance().getConnectStatus());
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (checkedId == R.id.rb_menu_dev_debugger) {
            //仪器调试
            mRoot.viewPagerOptionFragment.setCurrentItem(PAGE_ONE);
        } else if (checkedId == R.id.rb_menu_param_set) {
            //参数设置
            mRoot.viewPagerOptionFragment.setCurrentItem(PAGE_TWO);
        } else if (checkedId == R.id.rb_menu_network_debugger) {
            //网络调试
            mRoot.viewPagerOptionFragment.setCurrentItem(PAGE_THREE);
        } else if (checkedId == R.id.rb_menu_dev_info) {
            //设备信息
            mRoot.viewPagerOptionFragment.setCurrentItem(PAGE_FOUR);
        }
    }

    private ViewPager2.OnPageChangeCallback onPageChangeCallback = new ViewPager2.OnPageChangeCallback() {
        @Override
        public void onPageSelected(int position) {
            super.onPageSelected(position);
            switch (position) {
                case PAGE_ONE:
                    mRoot.rbMenuDevDebugger.setChecked(true);
                    break;
                case PAGE_TWO:
                    mRoot.rbMenuParamSet.setChecked(true);
                    break;
                case PAGE_THREE:
                    mRoot.rbMenuNetworkDebugger.setChecked(true);
                    break;
                case PAGE_FOUR:
                    mRoot.rbMenuDevInfo.setChecked(true);
                    break;
            }
        }
    };

    private void updateConnectImage(Integer status) {
        if (status == BluetoothProfile.STATE_CONNECTED) {
            Glide.with(this).load(R.mipmap.ico_bluetooth)
                    .into(mRoot.imgScan);
        } else {
            Glide.with(this).load(R.mipmap.ico_bluetooth_unconnect)
                    .into(mRoot.imgScan);
        }
    }
}